import { ui } from "../../ui/layaMaxUI";
import GameStage from "../../core_wq/game/GameStage";
import Game from "../../core_wq/game/Game";
import GameEvent from "../../core_wq/game/GameEvent";
import MonsterData from "../../core_wq/game/data/MonsterData";


export default class GameStageHpView extends ui.moduleView.main.GameStageHpViewUI {


    private _rectHp: Laya.Rectangle = new Laya.Rectangle();

    private _rectKill: Laya.Rectangle = new Laya.Rectangle();

    private _shapeHp: Laya.Sprite;

    private _init = false;

    constructor() {
        super();
        Laya.stage.on(GameEvent.REFRESH_KILL, this, this.onKillMonster);
    }

    onEnable() {
        if (this.killBar) {
            this._rectKill.x = 0
            this._rectKill.y = 0
            this._rectKill.width = 0;
            this._rectKill.height = 17;
            this.killBar.scrollRect = this._rectKill;
        }
    }


    onKillMonster() {
        if (!this.killBar) {
            return;
        }
        Laya.Tween.clearTween(this._rectKill);
        Laya.Tween.to(this._rectKill, { width: Game.gameStage.stageData.killMonsterCount / (Game.gameStage.stageVO.killMonsterCount) * this.killBar.width }, 300, null);
    }

    public reset() {
        this.hpBar.skin="images/common/hp4.png";
        Laya.Tween.to(this._rectHp, { width: this.hpBar.width }, 1000, null);
        Laya.Tween.to(this._rectKill, { width: 0 }, 1000, null);
        this._shapeHp&&this._shapeHp.removeSelf();
    }


    public updateHp(hurt: number): void {

        // console.log("GameStageHpView hurt",Game.gameStage.stageData.curHp,hurt)
        if (!this._shapeHp) {
            this._shapeHp = new Laya.Sprite();
        }
        Game.gameStage.stageData.curHp = Math.max(Game.gameStage.stageData.curHp, 0);
        if((Game.gameStage.stageData.curHp/Game.gameStage.stageData.maxHp)<0.1||Game.gameStage.stageData.curHp<=1){
            this.hpBar.skin="images/common/hp5.png";
        }
        else{
            this.hpBar.skin="images/common/hp4.png";
        }
        this._shapeHp.y = this.hpBar.y + 2;
        this._rectHp.x = 0
        this._rectHp.y = 0
        this._rectHp.height = 17;
        this._rectHp.width = Game.gameStage.stageData.curHp / Game.gameStage.stageData.maxHp * this.hpBar.width;
        this.hpBar.scrollRect = this._rectHp;
        // console.log("_monsterData ",this._monsterData)
        Laya.Tween.clearTween(this._shapeHp);
        this._shapeHp.graphics.clear();
        this._shapeHp.graphics.drawRect(0, 0, hurt / Game.gameStage.stageData.maxHp * this.hpBar.width, 15, "#ffffff");
        this.addChild(this._shapeHp);
        this._shapeHp.x = this.hpBar.x + this._rectHp.width;
        Laya.Tween.to(this._shapeHp, { width: 0 }, 300, null, new Laya.Handler(this, this.onCom));

    }

    private onCom(): void {
        this._shapeHp.removeSelf();
    }


}